package org.example.controller;

import org.example.annotation.CheckPermission;
import org.example.ourEnum.ExhibitionStatus;
import org.example.pojo.domain.Exhibition;
import org.example.pojo.vo.ExhibitionToManyArtifactVo;
import org.example.result.Result;
import org.example.service.ExhibitionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/exhibition")
@RestController
@CrossOrigin
public class ExhibitionController {

    @Autowired
    private ExhibitionService exhibitionService;

    // 通过id查看一个展览详细信息
    // 只有馆长和文物管理员可以操作
    @CheckPermission({"curator", "admin"})
    @GetMapping("/{id}")
    public Exhibition getExhibitionById(@PathVariable("id")  Integer id) {
        return exhibitionService.getExhibitionById(id);
    }

    // 查看所有已经公布的展览信息(不会加上展览上所展示文物的信息)
    // 所有人都可以操作
    @CheckPermission({"visitor","curator", "admin"})
    @GetMapping("/listPublished")
    public List<Exhibition> getPublishedExhibitions() {
        return exhibitionService.getPublishedExhibitions();
    }

    // 查看所有的展览信息(不会加上展览上所展示文物的信息)
    // 只有馆长和文物管理员可以操作
    @CheckPermission({"curator", "admin"})
    @GetMapping("/listAll")
    public List<Exhibition> getAllExhibitions() {
        return exhibitionService.getAllExhibitions();
    }

    // 添加一个展览
    // 只有馆长和文物管理员可以操作
    @CheckPermission({"curator", "admin"})
    @PostMapping("/add")
    public Result createExhibition(@RequestBody Exhibition exhibition) {
        int result = exhibitionService.createExhibition(exhibition);
        return result > 0 ? Result.success("Create Success") : Result.error("Create Failed");
    }

    // 更新一个展览的信息
    // 只有馆长和文物管理员可以操作
    @CheckPermission({"curator", "admin"})
    @PutMapping("/update")
    public Result updateExhibition(@RequestBody Exhibition exhibition) {
        int result = exhibitionService.updateExhibition(exhibition);
        return result > 0 ? Result.success("Update Success") : Result.error("Update Failed");
    }

    // 通过id删除一个展览
    // 只有馆长和文物管理员可以操作
    @CheckPermission({"curator", "admin"})
    @DeleteMapping("/{id}")
    public Result deleteExhibition(@PathVariable Integer id) {
        int result = exhibitionService.deleteExhibition(id);
        return result > 0 ? Result.success("Delete Success") : Result.error("Delete Failed");
    }

    // 通过展览的主题名字模糊查询到展览信息
    // 所有人都可操作
    @CheckPermission({"visitor","curator", "admin"})
    @GetMapping("/searchByTitle")
    public Result searchExhibitionsByTitle(@RequestParam("title") String title) {
        return Result.success("通过主题模糊查询成功",exhibitionService.searchExhibitionsByTitle(title));
    }

    // 通过展览的id查询展览的详细信息(会加上展览上所展出的文物信息)
    // 所有人都可操作
    @CheckPermission({"visitor","curator", "admin"})
    @GetMapping("/searchDetailById/{id}")
    public Result searchDetailById(@PathVariable("id")  Integer id) {
        return Result.success("查看展览会详细信息成功",exhibitionService.searchDetailById(id));
    }

    // 通过展览的id更改展览的状态(在展览准备完成后对展览进行公布)
    // 所有人都可操作
    @CheckPermission({"curator", "admin"})
    @PostMapping("/changeStatus")
    public Result changeStatus(@RequestBody Exhibition exhibition) {
        exhibitionService.changeStatus(exhibition.getExhibitionId(), ExhibitionStatus.published);
        return Result.success("change Success");
    }
}
